import java.net.URI;
import java.io.*;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class tracker
{
	public static void main(String[] args) throws IOException
	{
		InetSocketAddress addr = new InetSocketAddress("167.205.35.12",8080);
		HttpServer server = HttpServer.create(addr, 0);

		server.createContext("/", new MyHandler());
		server.setExecutor(Executors.newCachedThreadPool());
		server.start();
		System.out.println("Server : "+addr);
	}
}

class MyHandler implements HttpHandler
{
	public static TreeMap<String, Status> status = new TreeMap<String, Status>();
	public static TreeMap<String, String> file = new TreeMap<String, String>();
	public static TreeMap<String, String> hash = new TreeMap<String, String>();
	
	public void handle(HttpExchange exchange) throws IOException
	{
		Status temp;
		String requestMethod = exchange.getRequestMethod(),  URI =
		exchange.getRequestURI().toString(), local = exchange.getLocalAddress().toString(), remote = exchange.getRemoteAddress().toString();
		String[] value = new String[6];
		System.out.println(requestMethod);
		System.out.println("URL = "+local+URI);
		
		if (requestMethod.equalsIgnoreCase("GET"))
		{
			OutputStream responseBody = exchange.getResponseBody();
			OutputStreamWriter osw = new OutputStreamWriter(responseBody);
			Headers requestHeaders = exchange.getRequestHeaders();
			Headers responseHeaders = exchange.getResponseHeaders();
			responseHeaders.set("Content-Type", "text/plain");
				exchange.sendResponseHeaders(200, 0);
			Set<String> keySet = requestHeaders.keySet();
			Iterator<String> iter = keySet.iterator();
			while (iter.hasNext())
			{
				String key = iter.next();
				List values = requestHeaders.get(key);
				String s = key + " = " + values.toString() + "\n";
				//responseBody.write(s.getBytes());
			}
			value = getValue(URI);
			if (!status.containsKey(remote))
			{
				temp = new Status();
				if (value[5]!=null && value[5].equals("started"))
					status.put(remote, temp);
				else
					osw.write("failure_reason seharusnya start dulu\n");
			}
			else
			{
				temp = status.get(remote);
				if (temp.event==2)
				{
					if (value[5].equals("started"))
					{
						temp.event = 0;
						status.put(remote, temp);
					}
					else
						osw.write("failure_reason seharusnya start dulu\n");
				}
				else
				{
				}
			}
			osw.write("warning warning\n");
			osw.write("interval 10\n");
			osw.close();
			responseBody.close();
		}
	}
	
	private String[] getValue(String s)
	{
		String[] result = new String[6];
		String para;
		int i, j, k=2, l=0, m = s.length();
		for (i=2; i<m; ++i)
		{
			if (s.charAt(i)=='&')
			{
				para = s.substring(k,l);
				
				if (para.equals("file_name"))
					result[0] = s.substring(l+1,i);
				else if (para.equals("port"))
					result[1] = s.substring(l+1,i);
				else if (para.equals("uploaded"))
					result[2] = s.substring(l+1,i);
				else if (para.equals("downloaded"))
					result[3] = s.substring(l+1,i);
				else if (para.equals("left"))
					result[4] = s.substring(l+1,i);
				else if (para.equals("event"))
					result[5] = s.substring(l+1,i);
				k = i+1;
			}
			else if (s.charAt(i)=='=')
			{
				l = i;
			}
		}
		para = s.substring(k,l);
		if (para.equals("file_name"))
			result[0] = s.substring(l+1,i);
		else if (para.equals("port"))
			result[1] = s.substring(l+1,i);
		else if (para.equals("uploaded"))
			result[2] = s.substring(l+1,i);
		else if (para.equals("downloaded"))
			result[3] = s.substring(l+1,i);
		else if (para.equals("left"))
			result[4] = s.substring(l+1,i);
		else if (para.equals("event"))
			result[5] = s.substring(l+1,i);
		return result;
	}
}

class Status
{
	public int event, uploaded, downloaded, left;
	public void Status()
	{
		event= uploaded= downloaded= left=0;
	}
}
